<template>
  <div class="login">
    <div class="container">
      <div class="avatar">
        <img src="../assets/avatar.jpg" alt="" />
      </div>
      <div>
        <el-input
          v-model.trim="user.username"
          placeholder="请输入账号"
          prefix-icon="el-icon-user-solid"
        ></el-input>
        <el-input
          v-model.trim="user.password"
          type="password"
          placeholder="请输入密码"
          prefix-icon="el-icon-key"
        ></el-input>
        <el-button class="login-btn" type="primary" @click="loginFn"
          >登录</el-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { login } from '@/api';
export default {
  data() {
    return {
      user: { username: '', password: '' },
    };
  },
  methods: {
    loginFn() {
      if (!this.user.username && !this.user.password) {
        this.$message.warning('用户名或密码不能为空');
        return;
      }
      const loading = this.$loading({
        lock: true,
        text: 'Logining',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)',
      });
      setTimeout(() => {
        login(this.user).then((res) => {
          if (res.data.code == 1) {
            this.$message.success(res.data.msg);
            this.$router.push('/layout/index');
          } else {
            this.$message.error(res.data.msg);
          }
        });
        loading.close();
      }, 800);
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;
  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .el-input {
      margin: 15px 0px;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
